﻿/****************************** Module Header ******************************\
 * Module Name:  XMLHelper.cs
 * Module Version:  1.0.0 (2012-2-15)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to operate the file or the directory. It supplies some
 * public method to move a folder,copy a folder,replace a file and so on.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

namespace UCF.Core.Data
{
    using ORM;
    /// <summary>
    /// XML帮助类
    /// </summary>
    public class XMLHelper
    {
        /// <summary>
        /// 获取或设置XmlDocument
        /// </summary>
        public XmlDocument Document { get; set; }

        /// <summary>
        /// 加载XML字符串
        /// </summary>
        /// <param name="xmlstring"></param>
        public void LoadString(string xmlstring)
        {
            if (xmlstring.Substring(0, 5) != "<?xml") xmlstring = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + xmlstring;
            this.Document = new XmlDocument();
            this.Document.LoadXml(xmlstring);
        }
        /// <summary>
        /// 加载XML文件
        /// </summary>
        /// <param name="path"></param>
        public void Load(string path)
        {
            this.Document = new XmlDocument();
            this.Document.Load(path);
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="path"></param>
        public void SaveAs(string path)
        {
            if (path == null || path.Length == 0) return;
            FileHelper.FolderExists(path, true);
            this.Document.Save(path);
        }
        /// <summary>
        /// 获取单个节点
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public XmlNode SelectNode(string xpath)
        {
            return this.Document.DocumentElement.SelectSingleNode(xpath);
        }
        /// <summary>
        /// 获取节点列表
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public XmlNodeList SelectNodes(string xpath)
        {
            return this.Document.DocumentElement.SelectNodes(xpath);
        }
        /// <summary>
        /// 获取节点属性
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public XmlAttribute GetAttributeNode(string name)
        {
            return this.Document.DocumentElement.GetAttributeNode(name);
        }
        /// <summary>
        /// 获取属性的值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetAttribute(string name)
        {
            return this.Document.DocumentElement.GetAttribute(name);
        }
        /// <summary>
        /// 获取XML实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public T GetXMLEntity<T>(string xmlString = "") where T:new()
        {
            if (xmlString.Length > 0) this.LoadString(xmlString);
            if (!this.Document.HasChildNodes) return default(T);
            T t = new T();
            string sName; string sValue; XmlNode node;
            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                sName = pi.Name;
                node = this.Document.DocumentElement.SelectSingleNode(string.Format("/{0}/{1}", typeof(T).Name, sName));
                if (node == null) continue;
                sValue = node.InnerText;
                EntityHelper.SetPropertyValue<T>(t,pi,sValue);
            }
            return t;
        }
        /// <summary>
        /// 将XML数据转换成实体对象的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="xmlString"></param>
        public void XMLToEntity<T>(ref T t,string xmlString="")
        {
            if (xmlString.Length > 0) this.LoadString(xmlString);
            string sName; string sValue; XmlNode node;
            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                sName = pi.Name;
                node=this.Document.DocumentElement.SelectSingleNode(string.Format("/{0}/{1}",typeof(T).Name,sName));
                if(node==null) continue;
                sValue = node.InnerText;
                EntityHelper.SetPropertyValue<T>(t, pi, sValue);
            }
        }
        /// <summary>
        /// 将实体对象转换成XML数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public string EntityToXMLString<T>(ref T t)
        {
            StringBuilder sb = new StringBuilder();
            string sName; object oValue;
            sb.AppendFormat("<{0}>", typeof(T).Name);
            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                sName = pi.Name;
                oValue = pi.GetValue(t, null);
                sb.AppendFormat("<{0}>{1}</{0}>", sName, oValue);
            }
            sb.AppendFormat("</{0}>", typeof(T).Name);
            return sb.ToString();
        }
    }
}
